/** <!--------------------------------------------------------------------------------------------->
 * @file XXXXXXX.XXX
 *
 * GSvn - A Graphical Client for SVN
 * Copyright (C) 2012  Karl Krach, gsvn@bluespirit.la
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 <!---------------------------------------------------------------------------------------------> */


#include "RevisionGraph.h"
#include "GraphBranch.h"
#include <QDebug>

RevisionGraph::RevisionGraph( QWidget *parent ) : QGraphicsView( parent ), m_pBorder(0)
{
	setBackgroundBrush( QColor( 230, 230, 230 ) );
	setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
}



void RevisionGraph::resizeEvent(QResizeEvent *)
{
	scene()->setSceneRect ( 0, 0, viewport()->width(), viewport()->height() );
	setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);

	if( !m_pBorder )
		m_pBorder = scene()->addRect( sceneRect().adjusted( +10, +10, -10, -10 ) );

	m_pBorder->setRect( sceneRect().adjusted( +10, +10, -10, -10 ) );
	m_pBorder->setPen( QPen(QColor( Qt::white ), 0) );
	m_pBorder->setBrush( QColor( Qt::white ) );
	m_pBorder->setZValue( -200 );

	int iNextXPos = 5;
	foreach( QGraphicsItem* pItem, items() )
	{
		GraphBranch* pBranch = dynamic_cast<GraphBranch*>( pItem );
		if( pBranch )
		{
			pBranch->SetHeight( m_pBorder->boundingRect().height() );
			pItem->setPos( iNextXPos + pItem->boundingRect().width() / 2.0f, 10 );
			iNextXPos += pItem->boundingRect().width() + 10;
			pItem->update();
		}
	}
}


void RevisionGraph::wheelEvent( QWheelEvent* pEvent )
{
	bool bSuccessful = false;
	int numDegrees = pEvent->delta() / 8;
	int numSteps = numDegrees / 15;

	if (pEvent->orientation() == Qt::Vertical)
	{
		foreach( QGraphicsItem* pItem, items() )
		{
			GraphBranch* pBranch = dynamic_cast<GraphBranch*>( pItem );
			if( pBranch ) bSuccessful |= pBranch->Scroll(numSteps);
		}
	}

	if( bSuccessful ) pEvent->accept();
	else              pEvent->ignore();
}
